package com.ddch.udpsocket.util;

import java.util.Timer;
import java.util.TimerTask;

/**
 * created by 韦敏敏
 * on 2019/12/18
 */
public class HeartBeatTimer {
    private Timer timer;
    private TimerTask task;
    private OnScheduleListener mListener;
    public HeartBeatTimer(){
        timer = new Timer();
    }
    public void startTimer(long delay, long period){
        task = new TimerTask() {
            @Override
            public void run() {
                if(mListener != null){
                    mListener.onSchedule();
                }
            }
        };
        timer.schedule(task, delay, period);
    }
    public void exit(){
        if(timer != null)
            timer.cancel();
        if(task != null)
            task.cancel();
    }
    public interface OnScheduleListener{
        void onSchedule();
    }
    public void setOnScheduleListener(OnScheduleListener onScheduleListener){
        this.mListener = onScheduleListener;
    }
}
